package employeeOperations;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import storage.Employee;

import GUIMain.EmployeeEditor;

@SuppressWarnings("serial")
public class EmployeeTableModel extends AbstractTableModel{


	private String[] strgarray = {"test","test","test"};
	private Vector<String> columnNames = new Vector<String>(Arrays.asList(strgarray));
	private Vector<Vector<Object>> data = new Vector<Vector<Object>>();
	private boolean editable = false;
	
	public EmployeeTableModel(boolean editable){
		this.editable = editable;
	}
	@Override
	public int getColumnCount() {
		return columnNames.size();
	}

	@Override
	public int getRowCount() {
		return data.size();
	}

	@Override
	public Object getValueAt(int row, int col) {
		try{
			Object temp = data.get(row).get(col);
			return temp;
		}catch(ArrayIndexOutOfBoundsException e){
			return null;
		}
	}
	public ArrayList<String> getSelectedValues(){
		ArrayList<String> output = new ArrayList<String>();
		
		for(int i=0;i<data.size();i++){
			if((Boolean)getValueAt(i, 2)==true){
				output.add((String)getValueAt(i,0));
			}
		}
		return output;
	}
	public boolean addRow(Vector<Object> row){
		data.add(row);
		fireTableRowsInserted(data.size()-1, data.size());
		return true;
	}
	public boolean addColumnNames(Vector<String> row){
		columnNames = row;
		fireTableDataChanged();
		return true;
	}
	public boolean removeAllRows(){
		int removed = data.size();
		if(removed>0){
			data.removeAllElements();
		}
		fireTableRowsDeleted(0,removed);
		return true;
	}
	public boolean removeRow(int row){
		data.remove(row);
		syncData();
		fireTableRowsDeleted(row, row+1);
		return true;
	}
    public void setValueAt(Object value, int row, int col) {
        data.get(row).set(col, value);
        syncData();
        fireTableCellUpdated(row, col);
    }
	public boolean removeAllSelected(){
		
		for(int i=getRowCount()-1;i>=0;i--){
			if(data.get(i).get(2).equals(true)){
				data.remove(i);
			}
		}
		syncData();
		fireTableDataChanged();
		return true;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Class getColumnClass(int c) {
		if(getValueAt(0, c).getClass()==null){
			return String.class;
		}else{
	        return getValueAt(0, c).getClass();
		}
	}
	/**
	 * Only the third column is editable (select for removal)
	 */
    public boolean isCellEditable(int row, int col) {
        if (col > 1) {
            return true;
        } else {
            return false;
        }
    }
    public void syncData(){
    	if(editable){
        	ArrayList<Employee> empList = new ArrayList<Employee>();
        	for(int i=0;i<data.size();i++){
        		Employee e = new Employee((String)data.get(i).get(0),(String)data.get(i).get(1));
        		empList.add(e);
        	}
        	EmployeeEditor.syncData(empList);
    	}
    }
}
